package com.ruoyi.web.controller.zbg;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.ruoyi.system.domain.zbg.*;
import com.ruoyi.system.service.impl.*;
import com.ruoyi.web.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Api("上传平台数据")
@RestController
@RequestMapping("/zbg")
@Slf4j
public class ZbgController {

    @Autowired
    private CostDataServiceImpl costDataServiceImpl;
    @Autowired
    private WarningAndExceptionDataServiceImpl warningAndExceptionDataServiceImpl;
    @Autowired
    private AnnualCostDataServiceImpl annualCostDataServiceImpl;
    @Autowired
    private MonthlyRepairDataServiceImpl monthlyRepairDataServiceImpl;
    @ApiOperation("上传成本数据")
    @PostMapping("/upload/costMonth")

    public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("开始上传成本数据");
        if (file.isEmpty()) {
            return "文件为空，请选择一个文件";
        }
        log.info("开始上传文件");

        List<CostData> costDataList = new ArrayList<>();
        EasyExcel.read(file.getInputStream(), CostData.class, new ReadListener<CostData>() {
            @Override
            public void invoke(CostData costData, AnalysisContext analysisContext) {
                // 过滤掉空数据
                if (costData.getDistrict() != null && !costData.getDistrict().trim().isEmpty()) {
                    costDataList.add(costData);
                }

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                //写入数据
                costDataServiceImpl.saveAll(costDataList);
            }
        }).sheet().doRead();

        return "文件上传成功，数据已导入数据库";
    }

    @ApiOperation("上传年度成本数据和维修费")
    @PostMapping("/upload/annualCost")
    public Result importanualCostData(@RequestParam("file") MultipartFile file) {
       log.info("开始上传年度成本数据和维修费");
        if (file.isEmpty()) {
            return Result.failed( "文件为空，请选择一个文件");
        }
        try {
            List<AnnualCostData> annualCostDataList = EasyExcel.read(file.getInputStream(), AnnualCostData.class, null).sheet().doReadSync();
            //去除对象为空的数据
            annualCostDataList.removeIf(annualCostData -> annualCostData.getDistrict() == null || annualCostData.getDistrict().trim().isEmpty());
            annualCostDataServiceImpl.saveAll(annualCostDataList);
            return Result.success("导入成功");
        } catch (IOException e) {
            return Result.failed("导入失败:请检查是否数据没有输入 " + e.getMessage());
        }
    }


    @ApiOperation( "上传月度维修费数据")
    @PostMapping("/upload/monthlyRepair")
    public Result importMonthlyRepairData(@RequestParam("file") MultipartFile file) {
   log.info("开始上传月度维修费数据");
    if (file.isEmpty()) {
        return Result.failed( "文件为空，请选择一个文件");
    }
        try {
            List<MonthlyRepairData> monthlyRepairDataList = EasyExcel.read(file.getInputStream(), MonthlyRepairData.class, null).sheet().doReadSync();
            //去除对象为空的数据
            monthlyRepairDataList.removeIf(monthlyRepairData -> monthlyRepairData.getDistrict() == null || monthlyRepairData.getDistrict().trim().isEmpty());
            monthlyRepairDataServiceImpl.saveAll(monthlyRepairDataList);
            return Result.success("导入成功");
        } catch (IOException e) {
            return Result.failed("导入失败:请检查是否数据没有输入 " + e.getMessage());
        }
    }

    @Autowired
    private IndicatorDataServiceImpl indicatorDataService;
//导入指标数据
    @ApiOperation("上传指标数据")
    @PostMapping("/upload/indicator")
    public Result importIndicatorData(@RequestParam("file") MultipartFile file) {
   log.info("开始上传指标数据");
        if (file.isEmpty()) {
            return Result.failed( "文件为空，请选择一个文件");
        }
        try {
            List<IndicatorData> indicatorDataList = EasyExcel.read(file.getInputStream(), IndicatorData.class, null).sheet().doReadSync();

            //去除对象为空的数据
            indicatorDataList.removeIf(indicatorData -> indicatorData.getDistrict() == null || indicatorData.getDistrict().trim().isEmpty());
            indicatorDataService.saveAll(indicatorDataList);

            return Result.success("导入成功");
        } catch (IOException e) {
            return Result.failed("导入失败:请检查是否数据没有输入 " + e.getMessage());
        }
    }


    @ApiOperation("上传预警和异常数据")
    @PostMapping("/upload/warningAndException")
    public Result importData(@RequestParam("file") MultipartFile file) {
   log.info("开始上传预警和异常数据");
    if (file.isEmpty()) {
        return Result.failed( "文件为空，请选择一个文件");
    }
        try {
            List<WarningAndExceptionData> warningAndExceptionDataList = EasyExcel.read(file.getInputStream(), WarningAndExceptionData.class, null).sheet().doReadSync();
            //去除对象为空的数据
            warningAndExceptionDataList.removeIf(warningAndExceptionData -> warningAndExceptionData.getDistrict() == null || warningAndExceptionData.getDistrict().trim().isEmpty());
            warningAndExceptionDataServiceImpl.saveAll(warningAndExceptionDataList);
            return Result.success("导入成功");
        } catch (IOException e) {
            return Result.failed("导入失败:请检查是否数据没有输入 " + e.getMessage());
        }
    }
    /**
     * 获取首页统计信息
     */
    @ApiOperation( "获取首页统计信息")
    @GetMapping("/getStatistics")
    public Result getStatistics() {
        log.info("获取首页统计信息");
       HashMap<String,Object> result = monthlyRepairDataServiceImpl.getStatistics();
       return Result.success(result);
    }
    @Autowired
    private MaintenanceStaffDataServiceImpl maintenanceStaffDataService;
@ApiOperation("上传县级维护人员数据")
    @PostMapping("/upload/maintenanceStaff")
    public Result importMaintenanceStaffData(@RequestParam("file") MultipartFile file) {
       log.info("开始上传县级维护人员数据");
        try {
            List<MaintenanceStaffData> maintenanceStaffDataList = EasyExcel.read(file.getInputStream(), MaintenanceStaffData.class, null).sheet().doReadSync();
            //判断集合非空
            if (maintenanceStaffDataList == null || maintenanceStaffDataList.isEmpty()) {
                return Result.failed("导入失败: 请检查是否数据没有输入");
            }
            //去除对象为空的数据
            maintenanceStaffDataList.removeIf(maintenanceStaffData -> maintenanceStaffData.getDistrict() == null || maintenanceStaffData.getDistrict().trim().isEmpty());
            maintenanceStaffDataService.saveAll(maintenanceStaffDataList);
            return Result.success("导入成功");
        } catch (IOException e) {
            return Result.failed("导入失败: " + e.getMessage());
        }
    }




}
